$(document).ready(function () {


    $(".tablegrid").find('td table').addClass("paging");

    $(".tablegrid").attr('border', '0');

    $(".tablegrid").find('td table tbody tr:first').append("<td>Page<td>");

    //check show message
    if ($("#ctl00_BodyContent_lblmes").text() != "") {
        setTimeout('showSuccess( $("#ctl00_BodyContent_lblmes").text())', 100);
    }
			$('#login').show().animate({   opacity: 1 }, 1000);
			$('.logo').show().animate({   opacity: 1,top: '40%'}, 800,function(){			
			$('.logo').show().delay(1200).animate({   opacity: 1,top: '12%' }, 300,function(){
				$('.formLogin').animate({   opacity: 1,left: '0' }, 300);
				$('.userbox').animate({ opacity: 0 }, 200).hide();
			 });		

			  })	
			$(".on_off_checkbox").iphoneStyle();
			$('.tip a ').tipsy({gravity: 'sw'});
			$('.tip input').tipsy({ trigger: 'focus', gravity: 'w' });
		});	
	    $('.userload').click(function(e){
			$('.formLogin').animate({   opacity: 1,left: '0' }, 300);			    
			  $('.userbox').animate({ opacity: 0 }, 200,function(){
				  $('.userbox').hide();				
			   });
	    });
        	    
	    $('#ctl00_BodyContent_but_login').click(function (e) {
	        if ($('#ctl00_BodyContent_username_id').val() == "" || $('#ctl00_BodyContent_password').val() == "") {
	            showError("Please Input Username / Password");
	            $('.inner').jrumble({ x: 4, y: 0, rotation: 0 });
	            $('.inner').trigger('startRumble');
	            setTimeout('$(".inner").trigger("stopRumble")', 500);
	            setTimeout('hideTop()', 3000);
	            return false;
	        }
	        var datas = {
	            Username: $('#ctl00_BodyContent_username_id').val(),
	            Password: $('#ctl00_BodyContent_password').val(),
                Table: "Candidate"
	        };
	        $.ajax({
	            type: 'POST',
	            data: JSON.stringify(datas),
	            url: "ajax.asmx/checkLogin",
	            dataType: "json",
	            contentType: "application/json; charset=utf-8",
	            success: function (data) {
	                if (data.d == "success") {
	                    hideTop();
	                    loading('Checking', 1);
	                    setTimeout("unloading()", 1000);
	                    setTimeout("Login('ExamInfo.aspx')", 1000);
	                }
	                else {
	                    showError("Input Username / Password is wrong");
	                    $('.inner').jrumble({ x: 4, y: 0, rotation: 0 });
	                    $('.inner').trigger('startRumble');
	                    setTimeout('$(".inner").trigger("stopRumble")', 500);
	                    setTimeout('hideTop()', 3000);
	                    return false;
	                }
	            }
	        });
	        return false;
	    });


	    $('#ctl00_BodyContent_AdminLogin').click(function (e) {
	        if ($('#ctl00_BodyContent_username_id').val() == "" || $('#ctl00_BodyContent_password').val() == "") {
	            showError("Please Input Username / Password");
	            $('.inner').jrumble({ x: 4, y: 0, rotation: 0 });
	            $('.inner').trigger('startRumble');
	            setTimeout('$(".inner").trigger("stopRumble")', 500);
	            setTimeout('hideTop()', 2000);
	            return false;
	        }
	        var datas = {
	            Username: $('#ctl00_BodyContent_username_id').val(),
	            Password: $('#ctl00_BodyContent_password').val(),
	            Table: "Manager"
	        };
	        $.ajax({
	            type: 'POST',
	            data: JSON.stringify(datas),
	            url: "../ajax.asmx/checkLogin",
	            dataType: "json",
	            contentType: "application/json; charset=utf-8",
	            success: function (data) {
	                if (data.d == "success") {
	                    hideTop();
	                    loading('Checking', 1);
	                    setTimeout("unloading()", 2000);
	                    setTimeout("Login('Default.aspx')", 1000);
	                }
	                else {
	                    showError("Input Username / Password is wrong");
	                    $('.inner').jrumble({ x: 4, y: 0, rotation: 0 });
	                    $('.inner').trigger('startRumble');
	                    setTimeout('$(".inner").trigger("stopRumble")', 500);
	                    setTimeout('hideTop()', 2000);
	                    return false;
	                }
	            }
	        });
	        return false;
	    });

	    $('#ctl00_BodyContent_but_res').click(function (){
	        $('#ctl00_BodyContent_username_id').val("");
	        $('#ctl00_BodyContent_password').val("");
	        return false;
	    });


	    function Login(url)
        {	
	        $("#login").animate({   opacity: 1,top: '49%' }, 200,function(){
		            $('.userbox').show().animate({ opacity: 1 }, 500);
			        $("#login").animate({   opacity: 0,top: '60%' }, 500,function(){
				        $(this).fadeOut(200,function(){
				            $(".text_success").slideDown();
				            $("#successLogin").animate({opacity: 1,height: "200px"},500);   			     
				        });							  
			            })	
                })
            setTimeout("window.location.href='" + url + "'", 1000);
        }


	
$('#alertMessage').click(function(){
	hideTop();
});

function showError(str){
	$('#alertMessage').addClass('error').html(str).stop(true,true).show().animate({ opacity: 1,right: '10'}, 500);	
	
}

function showSuccess(str){
    $('#alertMessage').removeClass('error').html(str).stop(true, true).show().animate({ opacity: 1, right: '10' }, 500);
    setTimeout('hideTop()', 5000);
}

function hideTop(){
	$('#alertMessage').animate({ opacity: 0,right: '-20'}, 500,function(){ $(this).hide(); });	
}	
function loading(name,overlay) {  
	  $('body').append('<div id="overlay"></div><div id="preloader">'+name+'..</div>');
			  if(overlay==1){
				$('#overlay').css('opacity',0.1).fadeIn(function(){  $('#preloader').fadeIn();	});
				return  false;
		 }
	  $('#preloader').fadeIn();	  
 }
 function unloading() {  
		$('#preloader').fadeOut('fast',function(){ $('#overlay').fadeOut(); });
 }
